<template>
  <div>
    <Select
      v-model="sheng"
      placeholder="请选择省份"
      size="large"
      style="width:150px"
      class="marginR10"
      @on-change="doZ(sheng,'shiList')"
    >
      <Option v-for="(val,i) in shengList" :value="val.id" :key="i">{{val.name}}</Option>
    </Select>
    <Select
      v-model="shi"
      v-show="$app.judgeData(shiList)"
      placeholder="请选择市"
      size="large"
      style="width:150px"
      class="marginR10"
      @on-change="doZ(shi,'quList')"
    >
      <Option v-for="(val,i) in shiList" :value="val.id" :key="i">{{val.name}}</Option>
    </Select>
    <Select
      v-model="qu"
      v-show="$app.judgeData(quList) && (type==1 || type==3)"
      placeholder="请选择区"
      size="large"
      style="width:150px"
      @on-change="doY()"
    >
      <Option v-for="(val,i) in quList" :value="val.id" :key="i">{{val.name}}</Option>
    </Select>
  </div>
</template>


<script>
export default {
  name: "yZ",
  components: {},
  data() {
    let data = {
      sheng: "",
      shi: "",
      qu: "",
      shengText: "",
      shiText: "",
      quText: "",
      zip_code:""//邮政编码
    };

    let tool = {
      shengList: [],//省列表=》接口返回
      shiList: [],//市列表=》接口返回
      quList: []//区列表=》接口返回
    };
    return Object.assign(data, tool);
  },
  created() {
    $ajax.getAreaList(0, resData => {
      this.shengList = resData;
    });
  },
  //type 1省+市+区；2省+市；3公司名认证专用：省+市+区
  props: ["addObj","type"],
  mounted() {
  },
  watch:{
    addObj(newVal,oldVal){

      //2省+市 的时候执行判断 编辑专用，
      if(this.type==2){
        let {
          addObj:{
            people_province,
            people_city
          },
          shengList
        }=this;
        //编辑页面穿过来，一旦有值，证明是修改订单
        if($app.judgeData(people_province) && $app.judgeData(people_city)){

          //省循环
          for(let i=0; i<shengList.length;i++){
            if(people_province == shengList[i].name){
              this.shengText=people_province;//省份 名称赋值
              this.sheng=shengList[i].id;//省份 id赋值
              this.doZ(shengList[i].id,'shiList',()=>{
                let {
                  shiList
                }=this;

                //市循环
                for(let j=0; j<shiList.length;j++){
                  if(people_city == shiList[j].name){
                    this.shiText=people_city;//市 名称赋值
                    this.shi=shiList[j].id;//市 id赋值
                    return;
                  }
                }

              });
              return;
            }
          }
        }

      }
    }
  },
  methods: {
    doZ(id, xu,callBack) {
      if (xu == "shiList") {
        this.quList = [];
        this.qu = "";
      }

      if ($app.judgeData(id)) {
        $ajax.getAreaList(id, resData => {
          this[xu] = resData;
          callBack instanceof Function && callBack();
        });
      } else {
        this.quList = [];
        this.qu = "";
      }

      //市就触发 运算 只适用于 type=2,3，选择了第二级，市，就触发运算；type1，第三级区，才会运算
      if(xu=="quList" && (this.type==2 || this.type==3)){
        this.doY();
      }
    },
    //地址运算
    doY() {
      let { sheng, shi, qu, shengList, shiList, quList } = this;
      
      this.shengText = "";
      this.shiText = "";
      this.quText = "";
      this.zip_code="";

      if ($app.judgeData(quList) && $app.judgeData(shiList) && this.type==1) {
        shengList.forEach((val, i) => {
          val.id == sheng && (this.shengText = val.name);
        });
        shiList.forEach((val, i) => {
          val.id == shi && (this.shiText = val.name);
        });
        quList.forEach((val, i) => {
          if(val.id == qu){
            this.quText = val.name;
            this.zip_code=val.zip;
          }
        });
      }else if(this.type==2){
        shengList.forEach((val, i) => {
          val.id == sheng && (this.shengText = val.name);
        });
        shiList.forEach((val, i) => {
          val.id == shi && (this.shiText = val.name);
        });
      }else if(this.type==3){
        shengList.forEach((val, i) => {
          val.id == sheng && (this.shengText = val.name);
        });
        shiList.forEach((val, i) => {
          val.id == shi && (this.shiText = val.name);
        });
        quList.forEach((val, i) => {
          val.id == qu && (this.quText = val.name);
        });
      }



      console.log(this.shengText, this.shiText, this.quText);
      

      if(this.type==1){
        console.log('邮政编码', this.zip_code);
        //往父组件传值
        this.addObj.business_license_area=this.shengText+this.shiText+this.quText;
        this.addObj.zip_code=this.zip_code;
      }else if(this.type==2){
        //往父组件传值
        this.addObj.people_province=this.shengText;
        this.addObj.people_city=this.shiText;
      }else if(this.type==3){
        //往父组件传值
        this.addObj.people_province=this.shengText;
        this.addObj.people_city=this.shiText;
        this.addObj.people_area=this.quText;
      }
    }
  }
};
</script>

